﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Net.Sockets;
using System.Net;
using System.Threading;

namespace UDPRelayControl
{
	/// <summary>
	/// Interaction logic for MainWindow.xaml
	/// </summary>
	public partial class MainWindow : Window
	{
		UDPControlManager connectionManager;

		public MainWindow()
		{
			InitializeComponent();

			labelConnectionStatus.Content = "";
			buttonFirstRelay.Content = buttonSecondRelay.Content = buttonThirdRelay.Content = buttonFourthRelay.Content = ConstsAndTextMessages.TurnOnRelayText;

			groupBoxFirstRelay.IsEnabled = groupBoxSecondRelay.IsEnabled = groupBoxThirdRelay.IsEnabled = groupBoxFourthRelay.IsEnabled = false;

			OnSucessfullyConnectedToTheServer += new EventHandler<EventArgs>(MainWindow_OnSucessfullyConnectedToTheServer);
		}

		public bool connectedToServer = false;

		void MainWindow_OnSucessfullyConnectedToTheServer(object sender, EventArgs e)
		{
			connectedToServer = true;

			groupBoxFirstRelay.IsEnabled = groupBoxSecondRelay.IsEnabled = groupBoxThirdRelay.IsEnabled = groupBoxFourthRelay.IsEnabled = true;
			labelConnectionStatus.Content = "Connection successful!";
			buttonConnect.IsEnabled = false;
		}

		void connectionManager_OnReceivedMessage(object sender, OnReceivedMessageEventArgs e)
		{
			var message = e.Message;

			if (!connectedToServer)
			{
				connectedToServer = true;
			}

			for (int i = 0; i < 4; i++)
			{
				Button button = buttonFirstRelay;

				if (i == 2)
				{
					button = buttonSecondRelay;
				}
				else if (i == 1)
				{
					button = buttonThirdRelay;
				}
				else if (i == 0)
				{
					button = buttonFourthRelay;
				}

				switch (e.Message[i]) 
				{
					case '0': button.Content = ConstsAndTextMessages.TurnOnRelayText;  break;
					case '1': button.Content = ConstsAndTextMessages.TurnOffRelayText;  break;
				}
			}

			Console.WriteLine(message);
			Console.WriteLine();
		}

		private void buttonFirstRelay_Click(object sender, RoutedEventArgs e)
		{
			if (buttonFirstRelay.Content.ToString() == ConstsAndTextMessages.TurnOnRelayText)
			{
				connectionManager.SendCommand(Commands.FirstRelayOn);
			}
			else
			{
				connectionManager.SendCommand(Commands.FirstRelayOff);
			}
		}

		private void buttonSecondRelay_Click(object sender, RoutedEventArgs e)
		{
			if (buttonSecondRelay.Content.ToString() == ConstsAndTextMessages.TurnOnRelayText)
			{
				connectionManager.SendCommand(Commands.SecondRelayOn);
			}
			else
			{
				connectionManager.SendCommand(Commands.SecondRelayOff);
			}
		}

		private void buttonThirdRelay_Click(object sender, RoutedEventArgs e)
		{
			if (buttonThirdRelay.Content.ToString() == ConstsAndTextMessages.TurnOnRelayText)
			{
				connectionManager.SendCommand(Commands.ThirdRelayOn);
			}
			else
			{
				connectionManager.SendCommand(Commands.ThirdRelayOff);
			}
		}

		private void buttonFourthRelay_Click(object sender, RoutedEventArgs e)
		{
			if (buttonFourthRelay.Content.ToString() == ConstsAndTextMessages.TurnOnRelayText)
			{
				connectionManager.SendCommand(Commands.FourthRelayOn);
			}
			else
			{
				connectionManager.SendCommand(Commands.FourthRelayOff);
			}
		}

		private void textboxPort_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.Key == Key.Enter)
			{
				connectToTheServer();
			}
		}

		private void buttonConnect_Click(object sender, RoutedEventArgs e)
		{
			connectToTheServer();
		}

		private void connectToTheServer()
		{
			int port = 0;
			if (Int32.TryParse(textboxPort.Text, out port))
			{
				try
				{
					connectionManager = new UDPControlManager(textboxIP.Text, port);
					connectionManager.OnReceivedMessage += new EventHandler<OnReceivedMessageEventArgs>(connectionManager_OnReceivedMessage);

					connectionManager.SendCommand(Commands.RequestStatus);

					buttonConnect.IsEnabled = false;
					labelConnectionStatus.Content = "Connecting to server...";
				}
				catch (Exception ex)
				{
					labelConnectionStatus.Content = "Connection unsuccessful!";
					System.Windows.Forms.MessageBox.Show("No such host is known. Check the IP address once again.");
				}
			}
			else
			{
				labelConnectionStatus.Content = "Connection unsuccessful!";
				global::System.Windows.Forms.MessageBox.Show("Enter a valid port! It must contain numbers only!");
			}
		}

		private event EventHandler<EventArgs> OnSucessfullyConnectedToTheServer;
	}
}
